package com.tomcat.server;


import com.tomcat.utils.HttpHeaderUtils;
import com.tomcat.utils.ResourcesUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Locale;

/**
 * desc:
 *
 * @author tengfei wang
 * @version 1.0
 * @date Created in 24/9/2020
 */
public class Response implements HttpServletResponse {

    private OutputStream outputStream;

    private String appBase;


    public Response(OutputStream outputStream, String appBase) {
        this.outputStream = outputStream;
        this.appBase = appBase;
    }

    /**
     * 根据内容输出
     *
     * @param content 输出内容
     * @throws IOException 异常
     */
    public void outPut(String content) throws IOException {
        outputStream.write(content.getBytes());
    }

    /**
     * 根据路径获取静态资源，并输出
     *
     * @param path 取静态资源路径
     * @throws IOException 异常
     */
    public void getHtml(String path) throws IOException {
        //根据绝对路径获取文件
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            //输出文件内容
            ResourcesUtils.outPutResource(new FileInputStream(file), outputStream);
        } else {
            // 文件不存在输出404页面
            outPut(HttpHeaderUtils.httpStatus404());
        }

    }

    @Override
    public void addCookie(Cookie cookie) {

    }

    @Override
    public boolean containsHeader(String s) {
        return false;
    }

    @Override
    public String encodeURL(String s) {
        return null;
    }

    @Override
    public String encodeRedirectURL(String s) {
        return null;
    }

    @Override
    public String encodeUrl(String s) {
        return null;
    }

    @Override
    public String encodeRedirectUrl(String s) {
        return null;
    }

    @Override
    public void sendError(int i, String s) throws IOException {

    }

    @Override
    public void sendError(int i) throws IOException {

    }

    @Override
    public void sendRedirect(String s) throws IOException {
        //根据绝对路径获取文件
        File file = new File(appBase + s);
        if (file.exists() && file.isFile()) {
            //输出文件内容
            ResourcesUtils.outPutResource(new FileInputStream(file), outputStream);
        } else {
            // 文件不存在输出404页面
            outPut(HttpHeaderUtils.httpStatus404());
        }
    }

    public void sendRedirect(String basePath, String s) throws IOException {
        //根据绝对路径获取文件
        File file = new File(basePath + s);
        if (file.exists() && file.isFile()) {
            //输出文件内容
            ResourcesUtils.outPutResource(new FileInputStream(file), outputStream);
        } else {
            // 文件不存在输出404页面
            outPut(HttpHeaderUtils.httpStatus404());
        }
    }

    @Override
    public void setDateHeader(String s, long l) {

    }

    @Override
    public void addDateHeader(String s, long l) {

    }

    @Override
    public void setHeader(String s, String s1) {

    }

    @Override
    public void addHeader(String s, String s1) {

    }

    @Override
    public void setIntHeader(String s, int i) {

    }

    @Override
    public void addIntHeader(String s, int i) {

    }

    @Override
    public void setStatus(int i) {

    }

    @Override
    public void setStatus(int i, String s) {

    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        return null;
    }

    @Override
    public void setCharacterEncoding(String s) {

    }

    @Override
    public void setContentLength(int i) {

    }

    @Override
    public void setContentType(String s) {

    }

    @Override
    public void setBufferSize(int i) {

    }

    @Override
    public int getBufferSize() {
        return 0;
    }

    @Override
    public void flushBuffer() throws IOException {

    }

    @Override
    public void resetBuffer() {

    }

    @Override
    public boolean isCommitted() {
        return false;
    }

    @Override
    public void reset() {

    }

    @Override
    public void setLocale(Locale locale) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }
}
